package com.lagou.filter;

import com.lagou.util.MonitorUtil;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;

@Activate(group = CommonConstants.CONSUMER)
public class TPMonitorFilter implements Filter {

    @Override
    public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {

        //方法调用起始时间
        long startTime = System.currentTimeMillis();

        Result result = invoker.invoke(invocation);

        //方法调用耗时
        long costTime = System.currentTimeMillis() - startTime;

        //当前调用的方法名称
        String methodName = invocation.getMethodName();

        //将统计时间和对应的方法名称添加到统计信息中
        MonitorUtil.addMethodCostTime(startTime, costTime, methodName);

        return result;
    }
}
